/*
 * 用户修改交易
 * 参数   交易码：trancode=1013,
 * 登录名：loginName,
 * 用户名：userName,
 * 密码：：passWord,
 * 邮箱: email
 */

package org.ccams.tran;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.ccams.domain.TProductplan;

public class Tran18113 extends AbstractJsonTran {

	public static final String getAfterDateString(String start_date, int day_i) {
		Calendar day = Calendar.getInstance();
		int year = Integer.parseInt(start_date.substring(0, 4));
		int month = Integer.parseInt(start_date.substring(5, 7)) - 1;
		int date = Integer.parseInt(start_date.substring(8, 10));

		day.set(year, month, date);
		day.add(Calendar.DATE, day_i);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(day.getTime());
	}

	public int getDiffentOfDate(String beginTime, String endTime) {
		int day = 0;
		Date begin = null;
		Date end = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		// 将截取到的时间字符串转化为时间格式的字符串
		try {
			begin = sdf.parse(beginTime);
			end = sdf.parse(endTime);
			// 获得时间差，除以1000是为了转换成秒
			long between = (begin.getTime() - end.getTime()) / 1000;
			day = (int) between / (24 * 3600);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return day;
	}

	@SuppressWarnings("unchecked")
	public void buildJSONObject(Map model, JSONObject json,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String idt_productPlan = (String) request.getParameter("id");
		String address_name = (String) request.getParameter("address_name");
		String client_name = (String) request.getParameter("client_name");
		String kind = (String) request.getParameter("kind");
		String lvcai = (String) request.getParameter("lvcai");
		String bolibaiye = (String) request.getParameter("bolibaiye");
		String jiaceng = (String) request.getParameter("jiaceng");
		String number = (String) request.getParameter("number");
		String product = (String) request.getParameter("product");
		String start_date = (String) request.getParameter("start_date");
		String guage_arrive = (String) request.getParameter("guage_arrive");
		String remark = (String) request.getParameter("remark");
		String truth_date = (String) request.getParameter("truth_date");
		System.out.println("从页面获取的数据：" + start_date);

		address_name = (address_name != null) ? address_name : "";
		client_name = (client_name != null) ? client_name : "";
		kind = (kind != null) ? kind : "";
		lvcai = (lvcai != null) ? lvcai : "";
		bolibaiye = (bolibaiye != null) ? bolibaiye : "";
		jiaceng = (jiaceng != null) ? jiaceng : "";
		guage_arrive = (guage_arrive != null) ? guage_arrive : "";
		remark = (remark != null) ? remark : "";

		TProductplan tproductPlan = new TProductplan();
		tproductPlan.setIdt_productPlan(new Integer(idt_productPlan));
		tproductPlan.setAddress_name(address_name);
		tproductPlan.setClient_name(client_name);
		tproductPlan.setKind(kind);
		tproductPlan.setLvcai(lvcai);
		tproductPlan.setBolibaiye(bolibaiye);
		tproductPlan.setJiaceng(jiaceng);
		tproductPlan.setGuage_arrive(guage_arrive);
		tproductPlan.setRemark(remark);

		// 设置制造周期
		if (product == null || product.equals("")) {
			tproductPlan.setProduct(10);
		} else {
			tproductPlan.setProduct(new Integer(product));
		}
		// 设置接单日期
		if (start_date == null || start_date.equals("")) {
			start_date = "";
		} else {
			String myDate = start_date.substring(0, 4) + "-"
					+ start_date.substring(5, 7) + "-"
					+ start_date.substring(8, 10);
			System.out.println("myDate" + myDate);
			tproductPlan.setStart_date(myDate);

			// 设置计划完成日期
			tproductPlan.setPlan_date(getAfterDateString(start_date,
					tproductPlan.getProduct()));
		}
		// 设置数量
		if (number == null || number.equals("")) {
			tproductPlan.setNumber(0);
		} else {
			tproductPlan.setNumber(new Integer(number));
		}
		// 设置实际完成日期
		if (truth_date == null || truth_date.equals("")) {
			truth_date = "";
		} else {
			String myTruth_date = "20" + truth_date.substring(0, 2) + "-"
					+ truth_date.substring(3, 5) + "-"
					+ truth_date.substring(6, 8);
			tproductPlan.setTruth_date(myTruth_date);
		}
		// 设置进度日期
		if (tproductPlan.getPlan_date() == null
				|| tproductPlan.getPlan_date().equals("")
				|| tproductPlan.getTruth_date() == null
				|| tproductPlan.getTruth_date().equals("")) {
			tproductPlan.setGuage(0);
		} else {
			tproductPlan.setGuage(new Integer(this.getDiffentOfDate(
					tproductPlan.getPlan_date() + " 00:00:00", tproductPlan
							.getTruth_date()
							+ " 00:00:00")));
		}

		// 调试用时间
		System.out.println("开始时间:" + tproductPlan.getStart_date());
		System.out.println("进度日期：" + tproductPlan.getProduct());
		System.out.println("计划完成日期：" + tproductPlan.getPlan_date());
		System.out.println("实际完成日期：" + tproductPlan.getTruth_date());
		System.out.println("进度日期：" + tproductPlan.getGuage());

		HashMap<String, Object> jsonMap = new HashMap<String, Object>();
		boolean success = false;
		String message = "";
		int i = getCcamsService().getTproductplanDao().updateTproductplan(
				tproductPlan);
		System.out.println("修改返回值："+i);
		if (i == 1) {
			success = true;
			message = message + "生产计划修改成功.......";
		} else {
			success = false;
			message = message + "生产计划修改失败.......";
		}
		jsonMap.put("success", success);
		jsonMap.put("message", message);
		json.putAll(jsonMap);
	}
}
